package org.linlinjava.litemall.wx.config;

import com.paypal.api.payments.*;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CreateRenewalPlanOrder {

    private String clientId = "AWDvpQuY7aJ6grDJd-xmwhQgEpBsU6w5a6RPbmmAyIpHOXtyAY-KgVqqox2cRm2QTfchnc-XoNsaNLk4";
    private String clientSecret = "EP2kyv5jLp-uNGSrFxXGNcqULZR4U38BlS0Z9dVSVivxcYSf3S_HFJHIBmtXF4GaHpYEDYacYkLYMsI0";
    private String mode = "sandbox";

    public String create(String planId, String date) {
        APIContext context = new APIContext(clientId, clientSecret, mode);
        // 创建新的协议
        Agreement agreement = new Agreement();
        agreement.setName("Supernote会员");
        agreement.setDescription("黄金会员，多重优惠特权");
        agreement.setStartDate(date);

        // 设置续费协议
        Plan plan = new Plan();
        plan.setId(planId);
        agreement.setPlan(plan);

        // 添加付款方式
        Payer payer = new Payer();
        payer.setPaymentMethod("paypal");
        agreement.setPayer(payer);

        // 添加收货地址
        ShippingAddress shipping = new ShippingAddress();
        shipping.setLine1("集心路168号");
        shipping.setCity("shanghai");
        shipping.setState("上海市");
        shipping.setPostalCode("200000");
        shipping.setCountryCode("CN");
        agreement.setShippingAddress(shipping);

        // 创建协议
        try {
            agreement = agreement.create(context);
            for (Links links : agreement.getLinks()) {
                if ("approval_url".equals(links.getRel())) {
                    URL url = new URL(links.getHref());
                    System.out.println("url = " + url);
                    break;
                }
            }
        } catch (PayPalRESTException e) {
            System.err.println(e.getDetails());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,1);
        Date time=cal.getTime();
        String dateString = sf.format(time);
        //UTC时间，第二天早晨9点收取第一次费用
        dateString = dateString + " 09:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        String timestamp = df.format(date);
        System.out.println(timestamp);
        System.out.println(Instant.now().toString());
        new CreateRenewalPlanOrder().create("P-8GA94072C6811240T7R45ESA", timestamp.toString());
    }
}

